package net.huashitong.Project.service;


import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import net.huashitong.Project.dao.RelocateProgressDao;
import net.huashitong.Project.entity.RelocateProgress;
import net.huashitong.Project.filter.RelocateProgressFilter;
import net.huashitong.user.entity.BnpUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RelocateProgressService extends StringPKService<RelocateProgress> {

    @Autowired
    private RelocateProgressDao dao;
    @Autowired
    private SysTableService tableService;

    @Override
    protected BaseDao<RelocateProgress, String> getDao() {
        return dao;
    }
    @Override
    protected void beforeSave(RelocateProgress obj) {
        obj.setSysId(tableService.updateMaxSysId("t_bnp_relocate_progress", null));
    }

    public List<RelocateProgress> queryByProjectId(String projectId){
        return dao.queryByProjectId(projectId);
    }

    //添加征迁计划
    public void addRelocateProgress(RelocateProgressFilter filter, BnpUser bnpUser){
        RelocateProgressFilter f = new RelocateProgressFilter();
        f.setDataTime(filter.getDataTime());
        f.setProjectId(filter.getProjectId());
        List<RelocateProgress> list = dao.queryRelocateProgressByFilter(f);
        if (list.size() > 0) {
            //如果有该时间的数据则更新，否则新增
            RelocateProgress relocateProgress = list.get(0);
            filter.setSysId(relocateProgress.getSysId());
            BeanUtils.copyProperties(filter, relocateProgress);
            this.onPreUpdate(relocateProgress);
            this.beforeUpdate(relocateProgress);
            relocateProgress.setUpdUser(bnpUser.getSysId());
            this.updateObject(relocateProgress);
            this.afterUpdate(relocateProgress);
        }else{
            RelocateProgress relocateProgress = new RelocateProgress();
            BeanUtils.copyProperties(filter, relocateProgress);
            relocateProgress.setDataUnit("亩");
            relocateProgress.setProblemState("0");
            relocateProgress.setFillUser(bnpUser.getName());
            relocateProgress.setFillPhone(bnpUser.getPhone());
            relocateProgress.setFillDept(bnpUser.getBranch());
            this.onPrePersist(relocateProgress);
            this.beforeSave(relocateProgress);
            relocateProgress.setCreateUser(bnpUser.getSysId());
            this.saveObject(relocateProgress);
            this.afterSave(relocateProgress);
        }
    }

}
